﻿using System;
using System.IO;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net.Sockets; 

namespace TsamIRC
{
    class ClientSocket
    {
        private String _server;
        private int _port;
        private TcpClient _client = null;
        private NetworkStream _netStream = null;
        private StreamReader _streamReader;
        private byte[] _byteBuffer = null;
        private const int BUFFERSIZE = 512;

        /* --------------------------------------
         *              Constructor 
         * -------------------------------------- */
        public ClientSocket(String server, int port)
        {
            _server = server;
            _port = port;
        }

        /* ----------------------------------------------------
         * Initialize function.
         * Must be called after Constructor in order to connect
         * ---------------------------------------------------- */
        public bool initialize()
        {
            _client = new TcpClient(_server, _port);
            if (!_client.Connected)
            {
                return false;
            }
            _netStream = _client.GetStream();
            _streamReader = new StreamReader(_netStream);

            return true;
        }

        /* ----------------------------------------------------
         * Send Data function.
         * Used to send command/message string to the server.
         * ---------------------------------------------------- */
        public void sendData(String sendString)
        {
            sendString += "\r\n";
            _byteBuffer = Encoding.UTF8.GetBytes(sendString);
            _netStream.Write(_byteBuffer, 0, _byteBuffer.Length);
        }


        /* ----------------------------------------------------------------------
         * Receive Data function.
         * Used to receive data from the server. 
         * Places the Network stream into a StreamReader, which then outputs data
         * one line at a time to the user. Once the StreamReader is spent, it
         * reads from the server again and the process repeats itself.
         * ---------------------------------------------------------------------- */
        public String receiveData()
        {
            if (_streamReader.EndOfStream)
            {
                if (_netStream.Read(_byteBuffer, 0, BUFFERSIZE) == 0)
                {
                    Console.WriteLine("Connection closed prematurely.");
                    return null;
                }
                _streamReader = new StreamReader(_netStream);
            }
          
            return _streamReader.ReadLine();
        }

        public bool isConnected()
        {
            return _client.Connected;
        }

        /* -------------------------------------------------------
         * Close Connection function.
         * Should be called at when the client finishes execution
         * in order to cleanly close the connection to the server.
         * ------------------------------------------------------- */
        public void closeConnection()
        {
            if (_client != null && _client.Connected)
            {
                _netStream.Close();
                _client.Close();
            }
        }
    }
}
